#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define PORT_FILE	"/dev/port"
#define PORT_ADDR	0x378
#define PORT_DATA	(PORT_ADDR + 0)
#define PORT_STAT	(PORT_ADDR + 1)
#define PORT_CTRL	(PORT_ADDR + 2)

static int fd = -1;

static unsigned char io_read(unsigned int port)
{
	unsigned char data;
	lseek(fd, port, SEEK_SET);
	read(fd, &data, 1);
	return data;
}

static void io_write(unsigned int port, unsigned char data)
{
	lseek(fd, port, SEEK_SET);
	write(fd, &data, 1);
}

int main(void)
{
	char msg[32] = "pptest: use /dev/port\r\n";
	int i;

	fd = open(PORT_FILE, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "open %s failed: %s\n", PORT_FILE, strerror(errno));
		return -1;
	}
	fprintf(stdout, "opened %s\n", PORT_FILE);
	fprintf(stdout, "status 0x%02x\n", io_read(PORT_STAT));

	for (i = 0; i < strlen(msg); i++) {
		io_write(PORT_DATA, (unsigned char)msg[i]);
		io_write(PORT_CTRL, 0x0D); /* strobe */
		io_write(PORT_CTRL, 0x0C);
	}
	fprintf(stdout, "send message done\n");

	close(fd);
	return 0;
}
